package com.tr.rentcar.controller.houtai.car;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.tr.rentcar.common.FileUpload;
import com.tr.rentcar.common.StringUtils;
import com.tr.rentcar.model.dao.CarAttrDao;
import com.tr.rentcar.model.dao.CarBrandDao;
import com.tr.rentcar.model.dao.CarInfoDao;
import com.tr.rentcar.model.dao.CarStyleDao;
import com.tr.rentcar.model.dao.CarUseDao;

@WebServlet("/carmodify")
@MultipartConfig(maxFileSize = 10 * 1024 * 1024)
public class CarModifyControllor extends HttpServlet {
	CarBrandDao CarBrandDao = new CarBrandDao();
	CarAttrDao CarAttrDao = new CarAttrDao();
	CarInfoDao CarInfoDao = new CarInfoDao();
	CarStyleDao CarStyleDao = new CarStyleDao();
	CarUseDao CarUseDao = new CarUseDao();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		String pid = req.getParameter("pid");
		Integer pId = Integer.parseInt(pid);
		// 1是品牌管理修改
		if (pId == 1) {
			try {
				int panduan=Integer.parseInt(req.getParameter("panduan"));
				//System.out.println(panduan);
				String cbName = req.getParameter("cbName");
				String id = req.getParameter("id");
				if (panduan!=0) {
					//System.out.println("进来了");
					Part part = req.getPart("tx");
					//String fileSavePath = "D:/Users/Administrator/git/rentcar/WebContent/statics/image/upload/logo";
					String fileSavePath = "F:/git/rentcar/WebContent/statics/image/upload/logo";
					String fileName = FileUpload.upload(part, fileSavePath);
					CarBrandDao.update(cbName, fileName, Integer.parseInt(id));	
				}
				CarBrandDao.updatei(cbName, Integer.parseInt(id));
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			resp.sendRedirect(req.getContextPath() + "/houtaicarbrand");
		}
		/**
		 * 品牌管理添加
		 */
		if (pId == 11) {
			try {
				int panduan=Integer.parseInt(req.getParameter("panduan"));
				//System.out.println(panduan);
				String cbName = req.getParameter("cbName");
				//System.out.println("添加");
				if (panduan!=0) {
					String FILE_SAVE_ROOT_PATH = "D:/Users/Administrator/git/rentcar/WebContent/statics/image/upload/logo";
					Part part = req.getPart("tx");
					//String fileSavePath = "D:/Users/Administrator/git/rentcar/WebContent/statics/image/upload/logo";
					String fileSavePath = "F:/git/rentcar/WebContent/statics/image/upload/logo";
					String fileName = FileUpload.upload(part, fileSavePath);
					CarBrandDao.insert2(cbName, fileName);	
				}
				CarBrandDao.insert(cbName);;
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			resp.sendRedirect(req.getContextPath() + "/houtaicarbrand");
		}
		// 2类型修改
		if (pId == 2) {
			try {
				String name = req.getParameter("name");
				String csId = req.getParameter("csId");
				Integer csid = Integer.parseInt(csId);
				CarAttrDao.update(name, csid);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			resp.sendRedirect(req.getContextPath() + "/carattr");
		}
		// 3品牌值修改
		if (pId == 3) {
			Integer csid = null;
			try {
				String pan = req.getParameter("pan");
				String name = req.getParameter("name");
				String caid = req.getParameter("caId");
				String csId = req.getParameter("csId");
				if (!StringUtils.isEmpty(pan)) {
					System.out.println("这是添加"+csId+name);
					CarAttrDao.insert(name, csId);
					resp.sendRedirect(req.getContextPath() + "/carattrval?id=" + csId );
					return;
				}
				System.out.println(csId+"|"+caid);
				csid = Integer.parseInt(csId);
				CarAttrDao.update2(name, caid);
			} catch (Exception e) {
				e.printStackTrace();
			}
			resp.sendRedirect(req.getContextPath() + "/carattrval?id=" + csid + "");
		}
		/**
		 * 修改车辆信息
		 */
		if (pId == 4) {
			try {
				//System.out.println("修改");
				String id = req.getParameter("id");
				String cuid = req.getParameter("cuid");
				String cinfo = req.getParameter("cinfo");
				String cjingpin = req.getParameter("cjingpin");
				String cnew = req.getParameter("cnew");
				String chot = req.getParameter("chot");
				Integer Id = Integer.parseInt(id);
				if (cjingpin == null) {
					cjingpin = "1";
				}
				if (cnew == null) {
					cnew = "1";
				}
				if (chot == null) {
					chot = "1";
				}
				//System.out.println(cjingpin + chot + cnew);
				String cname = req.getParameter("cname");
				String cno = req.getParameter("cno");
				String csid = req.getParameter("csid");
				String cbid = req.getParameter("cbid");
				String cmoney1 = req.getParameter("cmoney");
				Double cmoney = Double.parseDouble(cmoney1);
				CarInfoDao.update1(cname, cno, cmoney, cinfo, Id);
				CarInfoDao.update2(csid, Id);
				CarInfoDao.update3(cbid, Id);
				CarInfoDao.update4(cuid, Id);
				CarInfoDao.update5(Id, cjingpin, cnew, chot);
			} catch (Exception e) {
				e.printStackTrace();
			}
			resp.sendRedirect(req.getContextPath() + "/carstore");
		}
		/**
		 * 添加车辆信息
		 */
		if (pId == 41) {
			try {
				String cphoto = req.getParameter("cphoto");
				String cuid = req.getParameter("cuid");
				String cinfo = req.getParameter("cinfo");
				String cjingpin = req.getParameter("cjingpin");
				String cnew = req.getParameter("cnew");
				String chot = req.getParameter("chot");
				if (cjingpin == null) {
					cjingpin = "1";
				}
				if (cnew == null) {
					cnew = "1";
				}
				if (chot == null) {
					chot = "1";
				}
				String cname = req.getParameter("cname");
				String cno = req.getParameter("cno");
				String csid = req.getParameter("csid");
				String cbid = req.getParameter("cbid");
				String cmoney1 = req.getParameter("cmoney");
				Double cmoney = Double.parseDouble(cmoney1);
				CarInfoDao.save1(cname, cno, cmoney, csid, chot, cnew, cjingpin, cinfo, cuid, cbid);
			} catch (Exception e) {
				e.printStackTrace();
			}
			resp.sendRedirect(req.getContextPath() + "/carstore");
		}
		/**
		 * 修改车辆图片
		 */
		if (pId == 5) {
			try {
				int panduan=Integer.parseInt(req.getParameter("panduan"));
				if (panduan!=0) {
				String id = req.getParameter("id");
				Integer Id = Integer.parseInt(id);
				//String FILE_SAVE_ROOT_PATH = "D:/Users/Administrator/git/rentcar/WebContent/statics/image/upload/cars";
				String FILE_SAVE_ROOT_PATH = "F:/git/rentcar/WebContent/statics/image/upload/cars";
				Part part = req.getPart("tx");
				String fileName = part.getSubmittedFileName();
				File uploadDir = new File(FILE_SAVE_ROOT_PATH);
				if (!uploadDir.exists()) {
					uploadDir.mkdirs();
				}
				String suffix = fileName.substring(fileName.lastIndexOf("."));
				String newFileName = UUID.randomUUID().toString() + suffix;
				String filePath = uploadDir + File.separator + newFileName;
				part.write(filePath);
				//System.out.println(newFileName);
				CarInfoDao.updatephoto(newFileName, Id);
				}
				resp.sendRedirect(req.getContextPath() + "/carstore");

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}
}
